// ==UserScript==
// @name         Micoua_B站工具箱
// @version      1.0.0
// @author       micoua
// @namespace    https://greasyfork.org/zh-CN/users/162781
// @description  B站工具箱

// @include      *bilibili.com*

//               ↓ jQuery核心文件 ↓
// @require      https://greasyfork.org/scripts/39025-micoua-jquery-min-js/code/Micoua_jQuery_min_js.js?version=255336
//               ↓ jQueryUI核心文件 ↓
// @require      https://greasyfork.org/scripts/40306-micoua-jqueryui-min-js/code/Micoua_jQueryUI_min_js.js?version=267377

// @grant        unsafeWindow
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_listValues
// ==/UserScript==

(function() {
    /** 是否最外 */
    if (self != top) { return; }

    /** 初始化JQ语法 */
    var $ = $ || window.$;

    /** 定义变量 */
    var curWebURL = window.location.href;
    var tipsMusic = "https://gitee.com/micoua/public-resources/raw/master/music/sound/cat.mp3";

    /** 基础入口 */
    function main() {
        // 添加工具
        // addTools();

        // 直播瓜子宝箱
        if (curWebURL.indexOf("https://live.bilibili.com/") != -1 && curWebURL != "https://live.bilibili.com/") {
            liveSeedsBox();
            // 添加音效标签
            $("body").append('<audio id="playMusic" src=""></audio>');
        }

        // QQ飞车手游直播
        if (curWebURL.indexOf("https://live.bilibili.com/21743919") != -1) {
            // 隐藏悬浮导航窗口
            $(".activity-navigator").css("display", "none");
            // 隐藏扭蛋抽奖和精彩集锦
            $("#app > section:nth-child(5)").css("display", "none");
            $("#app > section:nth-child(7)").css("display", "none");
            // 修改背景图
            $("#app > section:nth-child(2)").css("height", "20px");
            // 修改播放区
            $("#app > section:nth-child(3)").css("height", "670px");
            // 修改赛程
            $("#app > section:nth-child(4) > div.t-space-container").css("width", "100%");
            // 修复Bilibili Evolved插件导致导航栏不显示的bug
            $("#internationalHeader > div").removeClass("mini-header");
            // 修复战队积分榜样式bug
            $("#M49EGedM4T > div > div.game-rank-list.dp-i-block").css("float", "left");
            $("#M49EGedM4T > div > div.schedule-wrapper.p-relative.dp-i-block").css("margin-top", "70px");
        }
    }

    /** 直播瓜子宝箱 */
    var liveSeedsBox = function() {
        // 添加样式
        var styles = `<style>
            .toggleColor {
                background-color: #fff !important;
                color: rgb(251, 114, 153) !important;
                border: 1px solid rgb(251, 114, 153) !important;
            }
            #seedsBoxCountdown, #luckyTip, #luckyPartake {
                position: fixed;
                top: 5px;
                left: 5px;
                z-index: 9999999;
                background-color: rgb(251, 114, 153);
                color: #fff;
                padding: 4px 8px;
                border-radius: 10px;
                font-size: 12px;
                border: 1px solid #fff;
            }
            #luckyTip {
                background-color: rgb(137, 111, 243);
            }
            #luckyPartake {
                background-color: rgb(251, 101, 101);
                cursor: pointer;
            }
            #luckyPartake img {
                max-height: 200%;
                margin-top: -6px;
            }
        </style>`;
        $("head").append(styles);

        // 延时执行
        setTimeout(function() {
            var findTimes = 0;
            var colorTag = true;

            // 瓜子宝箱
            $("body").append("<div id='seedsBoxCountdown'></div>")
            var seedsBox = setInterval(function() {
                var timeText = $_(".count-down").text();
                var curTimeText = $_(".in-countdown span").text().replace(/ /g, "");
                var roundText = $_(".round-count span").text().replace(/ /g, "").substr(1, 3);
                var statusText = $_(".status-text").text();
                var showText = timeText + "（" + curTimeText + "）[" + roundText + "]";
                $("#seedsBoxCountdown").text(showText);
                if (statusText.indexOf("今天已经木有宝箱惹") != -1) {
                    $("#seedsBoxCountdown").css("display", "none");
                    window.clearInterval(seedsBox);
                } else if (timeText === "00:00") {
                    $_(".icon-btn[data-title=退出网页全屏]").click();
                    $_(".icon-btn[data-title=退出全屏]").click();
                    $_(".box-icon").click();
                    $_(".link-input").focus();
                    if (colorTag) {
                        if (findTimes % 5 === 0) { playMusic(tipsMusic, true); }
                        $("#seedsBoxCountdown").addClass("toggleColor");
                    } else {
                        $("#seedsBoxCountdown").removeClass("toggleColor");
                    }
                    colorTag = !colorTag;
                } else {
                    $("#seedsBoxCountdown").removeClass("toggleColor");
                }
                findTimes++;
            }, 1000);

            // 天选时刻
            var luckyText = "";
            var luckyCountText = "";
            var tipShowText = "";
            $("body").append("<div id='luckyTip'></div>");
            $("body").append("<div id='luckyPartake'><img src=''></div>");
            var luckyTime = setInterval(function() {
                if ($_(".anchor-lottery-entry").length <= 0) {
                    // 没有天选框
                    $("#luckyTip").css("display", "none");
                    $("#luckyPartake").css("display", "none");
                } else if ($_(".lottery-result").length > 0) {
                    // 已有天选结果
                    tipShowText = $_(".lottery-result").text() + "（" + luckyText + "）[" + luckyCountText + "]";
                } else if ($_("#app .dp-table-cell").length > 0) {
                    // 天选倒计时中
                    var timeText = $_(".anchor-lot-text").text();
                    var luckyInfos = $_("#app .dp-table-cell")[0].innerHTML;
                    luckyText = luckyInfos.substring(0, luckyInfos.indexOf("<"));
                    luckyCountText = luckyInfos.substring(luckyInfos.indexOf(">") + 1, luckyInfos.indexOf("</span>"));
                    tipShowText = timeText + "（" + luckyText + "）[" + luckyCountText + "]";
                } else if ($_(".anchor-lot-text").length > 0) {
                    // 有倒计时或已开奖提示(宝箱相邻)
                    tipShowText = $_(".anchor-lot-text").text() + "（" + luckyText + "）[" + luckyCountText + "]";;
                }
                $("#luckyTip").text(tipShowText);
                var $_btnName = $_(".btn-name")[0];
                if ($_btnName !== undefined) {
                    $("#luckyPartake img")[0].setAttribute("src", $_btnName.getAttribute("src"));
                    $("#luckyTip").css({
                        "display": "block",
                        "left": $("#seedsBoxCountdown").css("display") === "none" ? "5px" : $("#seedsBoxCountdown").width() + 30 + "px",
                    });
                    $("#luckyPartake").css({
                        "display": "block",
                        "left": $("#luckyTip").offset().left + $("#luckyTip").width() + 20 + "px",
                        "height": $("#luckyTip").height() + "px",
                    });
                };
            }, 1000);
            // 一键参与
            $("#luckyPartake").on("click", function() {
                $_(".btn-name").click();
            });

        }, 8 * 1000);
    }

    /** 添加工具 */
    var addTools = function() {
        // toastr通知工具
        var toastrStr = `<link crossorigin="anonymous" integrity="sha384-YzEqZ2pBV0i9OmlTyoz75PqwTR8If8GsXBv7HLQclEVqIC3VxIt98/U94ES6CJTR" href="https://lib.baomitu.com/toastr.js/2.1.4/toastr.min.css" rel="stylesheet">`;
        toastrStr += `<script crossorigin="anonymous" integrity="sha384-VDls8ImYGI8SwVxpmjX2Bn27U2TcNodzTNROTusVEWO55+lmL+H9NczoQJk6mwZR" src="https://lib.baomitu.com/toastr.js/2.1.4/toastr.min.js"></script>`;
        $("head").append(toastrStr);
        $(document).ready(function() {
            window.toastr.options = {
                "closeButton": false,
                "debug": false,
                "positionClass": "toast-top-right",
                "onclick": null,
                "showDuration": "300",
                "hideDuration": "1000",
                "timeOut": "0",
                "extendedTimeOut": "1000",
                "showEasing": "swing",
                "hideEasing": "linear",
                "showMethod": "fadeIn",
                "hideMethod": "fadeOut"
            }
        });
    }

    /** 播放音效 */
    var playMusic = function(path, play) {
        var audioEle = $("#playMusic")[0];
        audioEle.src = path;
        audioEle.volume = 0.3;
        audioEle.load();
        if (play) {
            audioEle.play();
        } else {
            audioEle.pause();
        }
    }

    /** jquery选择器 */
    var $_ = function(selector) {
        var parentSelector = "#player-ctnr";
        var giftParentSelector = "#gift-control-vm";
        var countdown = $(parentSelector + " iframe").contents().find(selector);
        if (countdown.length === 0) {
            countdown = $(giftParentSelector + " iframe").contents().find(selector);
        }
        if (countdown.length === 0) {
            countdown = $(parentSelector + " iframe").contents().find(giftParentSelector + " iframe").contents().find(selector);
        }
        return countdown.length === 0 ? $(selector) : countdown;
    }

    /** 加载完所有数据后进入主函数 */
    if (true) main();
})();